package com.cqndt.disaster.device.controller;

import java.util.Map;
import java.util.List;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.cqndt.disaster.device.domain.*;
import com.cqndt.disaster.device.util.Result;
import com.cqndt.disaster.device.vo.TabUserVo;
import com.cqndt.disaster.device.vo.TabDeviceVo;
import com.cqndt.disaster.device.vo.TabAlarmInfoVo;
import com.cqndt.disaster.device.service.TabDeviceService;
import com.cqndt.disaster.device.service.TabProjectService;
import org.springframework.web.bind.annotation.PostMapping;
import com.cqndt.disaster.device.service.TabAlarmInfoService;
import com.cqndt.disaster.device.service.TabAttachmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lgb
 * @date 2020-8-29
 * @说明  设备管理
 **/
@RestController
@RequestMapping("/tabDevice/")
public class TabDeviceController extends BaseController{
	
    @Autowired
    private TabDeviceService tabDeviceService;
    @Autowired
    private TabProjectService tabProjectService;
    @Autowired
    private TabAttachmentService tabAttachmentService;
    @Autowired
    private TabAlarmInfoService tabAlarmInfoService;
    /**
     * @param vo
     * @return
     **/
    @PostMapping(value="pointTabDevice")
    public Result pointTabDevice(TabDeviceVo vo){
        TabUserVo userVo = getCurrentUser();
        vo.setUserId(userVo.getId());
        if(null == vo.getLevel()|| vo.getLevel()==""){
            vo.setLevel(userVo.getLevel());
            vo.setAreaCode(userVo.getAreaCode());
        }
        vo.setStateTime(getOfflineTime());
        List<TabDeviceVo> list = tabDeviceService.listTabDevice(vo);
        Result result = new Result();
        result.setData(list);
        return result;
    }
    /**
     * @param vo
     * @return 设备列表
     **/
    @PostMapping(value="listTabDevice")
    public Result listTabDevice(TabDeviceVo vo){
    	TabUserVo userVo = getCurrentUser();
        vo.setUserId(userVo.getId());
        if(null == vo.getLevel()|| vo.getLevel()==""){
            vo.setLevel(userVo.getLevel());
            vo.setAreaCode(userVo.getAreaCode());
        }
        vo.setStateTime(getOfflineTime());
        PageHelper.startPage(vo.getPage(),vo.getLimit());
        PageInfo<TabDeviceVo> pageInfo = new PageInfo<TabDeviceVo>(tabDeviceService.listTabDevice(vo));
        Result result = new Result();
        result.setCount((int)pageInfo.getTotal());
        result.setData(pageInfo.getList());
        return result;
    }
    @PostMapping(value="getTabDevice")
    public Result getTabDevice(Integer id){
        Result result = new Result();
        TabDeviceVo vo = tabDeviceService.getTabDevice(id,getOfflineTime());
        result.setData(vo);
        return result;
    }
    @PostMapping(value="getTabProjects")
    public Result getTabProjects(){
    	Result result = new Result();
        List<Map<String,Object>> list = tabProjectService.selectByUserId(2);
        result.setData(list);
        return result;
    }
    /**
     * @param deviceNo 设备编号
     * @return 设备安装信息
     **/
    @PostMapping(value="getTabDeviceInstall")
    public Result getTabDeviceInstall(String deviceNo){
        Result result = new Result();
        TabDeviceInstall install = tabDeviceService.selectInstallByDeviceNo(deviceNo);
        result.setData(install);
        return result;
    }
    @PostMapping(value="getTabDeviceImg")
    public Result getTabDeviceImg(String deviceNo){
        Result result = new Result();
        TabDeviceInstallWithBLOBs install = tabDeviceService.selectInstallByDeviceNo(deviceNo);
        if(null != install){
            List<TabAttachment> list=tabAttachmentService.selectAttachments(install.getImgIds());
            result.setData(list);
        }
        return result;
    }
    
    /**
     * @param deviceNo 设备编号
     * @return 设备巡查记录
     **/
    @PostMapping(value="getTabDeviceCheck")
    public Result getTabDevice(String deviceNo){
        Result result = new Result();
        List<TabDeviceCheck> check=tabDeviceService.selectByDeviceNo(deviceNo);
        result.setData(check);
        return result;
    }
    /**
     * @param page 页数
     * @param limit 条数
     * @param deviceType 设备类型
     * @return 根据设备类型获取相应的告警
     **/
    @PostMapping(value="getAlarmsByMonitorId")
    public Result getAlarmsByMonitorType(TabAlarmInfoVo vo){
        PageHelper.startPage(vo.getPage(),vo.getLimit());
        PageInfo<Map<String,Object>> pageInfo = new PageInfo
        		<Map<String,Object>>(tabAlarmInfoService.TypelistTabAlarmInfo(vo));
        Result result = new Result();
        result.setCount((int)pageInfo.getTotal());
        result.setData(pageInfo.getList());
        return result;
    }
    
    @PostMapping(value="getAlarmsLevelByMonitorType")
    public Result getAlarmsLevelByMonitorType(TabAlarmInfoVo vo){
        List<Map<String,Object>> alarmsLevelByMonitorType = 
        		tabAlarmInfoService.getAlarmsLevelByMonitorType(vo);
        Result result = new Result();
        result.setData(alarmsLevelByMonitorType);
        return result;
    }
    
}